<?php

namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\AdRequest;
use App\Http\Resources\AdResource;
use App\Models\Ad;
use Illuminate\Http\Request;
class AdController extends Controller
{
    public function index(AdRequest $request){
        AdResource::wrap("data");
        $where['pid'] = $request->pid;
        $where['enabled'] = 1;
        $ad_list = Ad::where($where)->orderBy('orderby',"desc")->get();
        return AdResource::collection($ad_list);
    }

    //
    public function list(Request $request){
        AdResource::wrap("data");
//        $pids = $request->pid;
//        $pids = explode(",",$pids);
        $pids = [7,8,9,10,11,12,13,14,15,16];
        $ad_list = Ad::whereIn("pid",$pids)->where('enabled',1)->get();
       if (count($ad_list)>0){
           foreach($ad_list as $key=>$val){
               //首页跳转的广告页的banner
               if ($val['pid']==7){
                   $new_ad_list['banner'][] = new AdResource($val);
               }
               //首页跳转的广告页的新品大图
               if ($val['pid']==8){
                   $new_ad_list['new_big_ad'] = new AdResource($val);
               }
               //首页跳转的广告页的新品竖幅大图
               if ($val['pid']==9){
                   $new_ad_list['new_big_s_ad'] = new AdResource($val);
               }
               //首页跳转的广告页的新品竖幅大图右侧图1
               if ($val['pid']==10){
                   $new_ad_list['new_big_s_right_one_ad'] = new AdResource($val);
               }
               //首页跳转的广告页的新品竖幅大图右侧图2
               if ($val['pid']==11){
                   $new_ad_list['new_big_s_right_two_ad'] = new AdResource($val);
               }
               //首页跳转的广告页的新品常规图
               if ($val['pid']==12){
                   $new_ad_list['new_normal_ad'][] = new AdResource($val);
               }
               //首页跳转的广告页的特惠竖幅大图
               if ($val['pid']==13){
                   $new_ad_list['th_s_ad'] = new AdResource($val);
               }
               //首页跳转的广告页的特惠竖幅大图左侧小图1
               if ($val['pid']==14){
                   $new_ad_list['th_s_left_one_ad'] = new AdResource($val);
               }
               //首页跳转的广告页的特惠竖幅大图左侧小图2
               if ($val['pid']==15){
                   $new_ad_list['th_s_left_two_ad'] = new AdResource($val);
               }
               //首页跳转的广告页的特惠常规图
               if ($val['pid']==16){
                   $new_ad_list['th_normal_ad'][] = new AdResource($val);
               }
           }
        }else{
           $new_ad_list = [];
       }

        return response()->json($new_ad_list);
//        return AdResource::collection($ad_list);
    }
}
